/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.product.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.category.entity.WxCategory;
import com.thinkgem.jeesite.modules.category.service.WxCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.product.entity.WxProduct;
import com.thinkgem.jeesite.modules.product.service.WxProductService;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品管理Controller
 * @author 李云涛
 * @version 2017-09-03
 */
@Controller
@RequestMapping(value = "${adminPath}/product/wxProduct")
public class WxProductController extends BaseController {

    //查询类别
    @Autowired
    private WxCategoryService wxCategoryService;

	@Autowired
	private WxProductService wxProductService;
	
	@ModelAttribute
	public WxProduct get(@RequestParam(required=false) String id) {
		WxProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = wxProductService.get(id);
		}
		if (entity == null){
			entity = new WxProduct();
		}
		return entity;
	}
	
	@RequiresPermissions("product:wxProduct:view")
	@RequestMapping(value = {"list", ""})
	public String list(WxProduct wxProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WxProduct> page = wxProductService.findPage(new Page<WxProduct>(request, response), wxProduct); 
		model.addAttribute("page", page);
		return "modules/product/wxProductList";
	}

    @RequiresPermissions("product:wxProduct:view")
    @RequestMapping(value = "form")
    public String form(WxProduct wxProduct, Model model) {
        model.addAttribute("wxProduct", wxProduct);
        WxCategory wxCategory=new WxCategory();
        List<WxCategory> list=wxCategoryService.findList(wxCategory);
        List<WxCategory> industry=new ArrayList<WxCategory>();
        List<WxCategory> catagory=new ArrayList<WxCategory>();
        for(WxCategory m:list){
            if(m.getType().equals("1"))
                industry.add(m);
            else if(m.getType().equals("2"))
                catagory.add(m);
        }

        model.addAttribute("industry",industry);
        model.addAttribute("catagory",catagory);
        return "modules/product/wxProductForm";
    }


	@RequiresPermissions("product:wxProduct:edit")
	@RequestMapping(value = "save")
	public String save(WxProduct wxProduct, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, wxProduct)){
			return form(wxProduct, model);
		}
		wxProductService.save(wxProduct);
		addMessage(redirectAttributes, "保存产品管理功能成功");
		return "redirect:"+Global.getAdminPath()+"/product/wxProduct/?repage";
	}
	
	@RequiresPermissions("product:wxProduct:edit")
	@RequestMapping(value = "delete")
	public String delete(WxProduct wxProduct, RedirectAttributes redirectAttributes) {
		wxProductService.delete(wxProduct);
		addMessage(redirectAttributes, "删除产品管理功能成功");
		return "redirect:"+Global.getAdminPath()+"/product/wxProduct/?repage";
	}

}